/**
 * 
 */
package de.cbf.cam.dsa.battle.ui.windows.attackWindow;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import de.cbf.cam.dsa.model.ModelBase;
import de.cbf.cam.dsa.model.entities.BattleChar;
import de.cbf.cam.dsa.model.entities.OneNPC;
import de.cbf.cam.dsa.model.entities.constants.ManeuverConstants;
import de.cbf.cam.dsa.model.entities.maneuvers.OneManeuver;
import de.cbf.cam.dsa.model.entities.maneuvers.OneManeuverSelected;
import de.cbf.cam.dsa.model.entities.weapon.OneWeapon;
import de.cbf.cam.dsa.model.entities.weapon.WeaponInUse;

/**
 * @author Nebelritter
 *
 */
public class SideAttacker extends Composite {
	
	/**
	 * @uml.property  name="labelNameWeapon"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Label labelNameWeapon;
	/**
	 * @uml.property  name="labelATValue"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Label labelATValue;
	/**
	 * @uml.property  name="labelTPValue"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Label labelTPValue;
	/**
	 * @uml.property  name="labelNameAttacker"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Label labelNameAttacker;
	/**
	 * @uml.property  name="labelWeaponTalent"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Label labelWeaponTalent;
	/**
	 * @uml.property  name="tableManeuvers"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="de.cbf.cam.dsa.model.entities.maneuvers.OneManeuver"
	 */
	private Table tableManeuvers;
	/**
	 * @uml.property  name="attackerEffects"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private org.eclipse.swt.widgets.List attackerEffects; 
	
	/**
	 * @uml.property  name="tableAttackerManeuversSelection"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.awt.event.ActionListener"
	 */
	private List<ActionListener> tableAttackerManeuversSelection = new ArrayList<ActionListener>();
	
	private static final Color GRAY =Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY);
	
	public SideAttacker(Composite parent, int style) {
		super(parent, style);
		GridLayout layout4f =new GridLayout(4,false);
		layout4f.marginBottom = 3;
		layout4f.marginTop	  = 1;
		layout4f.marginLeft   = 3;
		layout4f.marginRight  = 3;
		layout4f.marginWidth  = 0;
		layout4f.marginHeight = 0;
		setLayout(layout4f);
		
		//TODO replace this with Composite to display character whole
		//name of atacker
		Label labelStaticName = new Label(this,SWT.NONE);
		labelStaticName.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false,1,1));
		labelStaticName.setText("Name:");

		labelNameAttacker = new Label(this,SWT.NONE);
		labelNameAttacker.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false,3,1));
		
		//name of attacker weaponMain
		Label labelStaticWeapon = new Label(this,SWT.NONE);
		labelStaticWeapon.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false,1,1));
		labelStaticWeapon.setText("Waffe:");

		labelNameWeapon = new Label(this,SWT.NONE);
		labelNameWeapon.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false,3,1));
		
		Label labelStaticWeaponTalent = new Label(this,SWT.NONE);
		labelStaticWeaponTalent.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false,1,1));
		labelStaticWeaponTalent.setText("Waffentalent:");

		labelWeaponTalent = new Label(this,SWT.NONE);
		labelWeaponTalent.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false,3,1));
	 		
		
		Composite cpWeaponInfo = new Composite(this,SWT.NONE);
			cpWeaponInfo.setLayoutData(new GridData(SWT.TOP,SWT.FILL,false,false,4,1));
			GridLayout layout = new GridLayout(4,false);
			layout.marginBottom = 0;
			layout.marginTop	 = 0;
			layout.marginLeft 	 = 0;
			layout.marginRight  = 0;
			layout.marginWidth  = 0;
			layout.marginHeight = 0;
			cpWeaponInfo.setLayout(layout);
			
			Label labelStaticAT = new Label(cpWeaponInfo,SWT.NONE);
			labelStaticAT.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false,1,1));
			labelStaticAT.setText("AT: ");
			
			labelATValue = new Label(cpWeaponInfo,SWT.NONE);
			labelATValue.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false,1,1));
			
			
			Label labelStaticTP = new Label(cpWeaponInfo,SWT.NONE);
			labelStaticTP.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false,1,1));
			labelStaticTP.setText("  TP: ");
			
			labelTPValue = new Label(cpWeaponInfo,SWT.NONE);
			labelTPValue.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false,1,1));
		// end todo
		
		tableManeuvers = new Table(this, SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL|SWT.SINGLE|SWT.CHECK|SWT.FULL_SELECTION);
		tableManeuvers.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,4,3));
		tableManeuvers.setHeaderVisible(true);
		tableManeuvers.setLinesVisible(true);
		tableManeuvers.addSelectionListener(new AttackerManeuverSelection());
		
			TableColumn column = new TableColumn(tableManeuvers, SWT.CENTER);
			column.setText("!");
			column.setWidth(22);
			
			column = new TableColumn(tableManeuvers, SWT.CENTER);
			column.setText("Option");
			column.setWidth(150);
		
			column = new TableColumn(tableManeuvers, SWT.CENTER);
			column.setText("Wert");
			column.setWidth(40);
		
		attackerEffects = new org.eclipse.swt.widgets.List(this, SWT.READ_ONLY|SWT.BORDER);
		attackerEffects.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,4,1));
	}
	
	
	public void setAttacker(BattleChar attacker){
		if(attacker != null){
			labelNameAttacker.setText(attacker.getCharacter().getName());
						
			if(attacker.getMainWeapon() != null){
				labelATValue.setText(attacker.getWeapon(OneNPC.Hand_Main).getATtack()+"");
				WeaponInUse weapon= attacker.getMainWeapon();
				if (weapon instanceof OneWeapon) {
					OneWeapon weaponMain = (OneWeapon) weapon;
					labelNameWeapon.setText(weaponMain.getName());
//					labelWeaponTalent.setText(attacker.getCharacter().getWeapon().getActiveWeaponTalent().getName());				
					labelTPValue.setText(attacker.getWeapon(OneNPC.Hand_Main).getWeapon().getTpString());
				}				
			}
			//initial maneuvers			
			List<OneManeuver> maneuvers = ModelBase.getInstance()
				.getManeuversForBattleCharacterByType(attacker,getSelectedManeuvers(),ManeuverConstants.TYPE_AT);
			
			setAttackerManeuvers(getSelectedManeuvers(),maneuvers,new ArrayList<OneManeuver>());
		}		
	}	
	
	public void setAttackerManeuvers(List<OneManeuver> selected, List<OneManeuver> enabled,List<OneManeuver> disabled){
		tableManeuvers.removeAll();
		addAttackerManeuvers(selected,ManeuverConstants.STATE_SELECTED);
		addAttackerManeuvers(enabled ,ManeuverConstants.STATE_ENABLED);
		addAttackerManeuvers(disabled,ManeuverConstants.STATE_DISABLED);
	}
	
	private void addAttackerManeuvers(List<OneManeuver> maneuvers,int state) {		
		for(int i =0;i<maneuvers.size();i++){
			OneManeuver maneuver = maneuvers.get(i);
			TableItem item = new TableItem(tableManeuvers, SWT.NONE);
			item.setText(0, "");
			item.setText(1,maneuver.getManeuverName());
			if(maneuver.getMinModifier()>=0){
				item.setText(2,maneuver.getMinModifier().toString());
			}				
			item.setData(maneuver);			
			switch (state) {
				case ManeuverConstants.STATE_DISABLED:{
					item.setGrayed(true);	
					item.setForeground(GRAY);
				break;
				}
				case ManeuverConstants.STATE_SELECTED:{
					item.setChecked(true);
				break;
				}
			}
		}		
	}
	
	public void setAttackerEffects(List<String> effects){
		attackerEffects.removeAll();
		for (String effect : effects) {
			attackerEffects.add(effect);
		}
	}
	
	public boolean registerTableAttackerManeuverSelectionListener(ActionListener listener){
		return tableAttackerManeuversSelection.add(listener);
	}
	public boolean deregisterTableAttackerManeuverSelectionListener(ActionListener listener){
		return tableAttackerManeuversSelection.remove(listener);
	}
	
	public OneManeuverSelected getActiveManeuver() {
		if(tableManeuvers.getSelectionCount()>0){
			TableItem item = tableManeuvers.getSelection()[0];
			Object selection = item.getData();			
			OneManeuver maneuver = (OneManeuver)selection;
			OneManeuverSelected mauneuverSelected = new OneManeuverSelected();
			mauneuverSelected.setManeuver(maneuver);
			mauneuverSelected.setSelected(item.getChecked());
			return mauneuverSelected ;
		}
		return null;//TODO better throw exception
	}
	
	public List<OneManeuver> getSelectedManeuvers() {
		List<OneManeuver> result = new ArrayList<OneManeuver>();
		for(int i =0;i<tableManeuvers.getItemCount();i++){
			TableItem item =tableManeuvers.getItem(i);
			if(item.getChecked()){
				result.add((OneManeuver)item.getData());
			}
		}
		return result;
	}
	
	public List<OneManeuver> getAllManeuvers() {
		List<OneManeuver> result = new ArrayList<OneManeuver>();
		for(int i =0;i<tableManeuvers.getItemCount();i++){
			TableItem item =tableManeuvers.getItem(i);			
			result.add((OneManeuver)item.getData());			
		}
		return result;
	}
	
	private class AttackerManeuverSelection extends SelectionAdapter{
		@Override
		public void widgetSelected(SelectionEvent e) {
//			if (e.item instanceof TableItem) {
//				TableItem item = (TableItem) e.item;
//				//make sure correct entry is selected/active
//				TableItem[] selecteds = tableManeuvers.getSelection();
//				if(selecteds.length>0){					
//					if(!item.equals(selecteds[0])){
//						tableManeuvers.select(tableManeuvers.indexOf(item));
//					}				
//				}else{
//					tableManeuvers.select(tableManeuvers.indexOf(item));
//				}
//			}			
			ActionEvent event = new ActionEvent(SideAttacker.this, ActionEvent.ACTION_PERFORMED, "attackerManeuversSelected");
			for (ActionListener listener : tableAttackerManeuversSelection) {
				listener.actionPerformed(event);
			}
		}		
	}

	

	
}
